<template>
    <view>
		<block v-if="(lang_info || null) != null">
			<!-- #ifdef APP-PLUS -->
			<!-- 右侧icon -->
			<view class="top-share-right-icon">
			    <uni-icons type="redo" size="20" color="#333" class="icon round cp" @tap="popup_share_event"></uni-icons>
			</view>
			<!-- #endif -->
			
			<!-- 轮播 -->
			<component-banner :propData="banner_list"></component-banner>

			<!-- 标题 -->
			<view class="cutprice-index-title">
				<view class="nav-title">
					<image class="nav-icon" :src="common_static_url+'mini/kanjia.png'" mode="aspectFit"></image>
					<block v-if="(base_info || null) != null">
						<text class="text-wrapper" v-if="(base_info.kanjia_name || null) != null">{{base_info.kanjia_name}}</text>
						<text class="text-wrapper" v-else>{{$t('fengcheche.cutprice.index_big_title')}}</text>
						<text class="text-wrapper2" v-if="(base_info.kanjia_fname || null) != null && base_info.kanjia_fname != ''">/{{ base_info.kanjia_fname || '' }}</text>
					</block>
					<block v-else>
						<text class="text-wrapper">{{$t('fengcheche.cutprice.index_big_title')}}</text>
						<text class="text-wrapper2">/{{$t('fengcheche.cutprice.index_big_ftitle')}}</text>
					</block>
				</view>
			</view>
			<!-- 列表 -->
			<view v-if="(data_list || nul) != null && data_list.length > 0" :class="'data-list cutprice-box-'+$t('fengcheche.cutprice.app_lang')">
				<view class="items bg-white" v-for="(item, index) in data_list" :key="item.key">
					<navigator :url="'/pages/fcc/cutprice/detail/detail?cut_id=' + item.id" hover-class="none">
						<image :src="item.images" mode="aspectFill" />
						<view class="base">
							<view class="tj-multi-line-hiding">{{ item.title }}</view>
							<view class="ftitle pr">
								<image class="pa img2" :src="common_static_url+'mini/hot.png'" />
								<text class="cr-grey">{{$t('fengcheche.cutprice.cutprice_have_got_desc_before')}} {{ item.have_got }} {{$t('fengcheche.cutprice.cutprice_have_got_desc')}}</text>
							</view>
							<view class="pininfo">
								<text class="t1">{{$t('fengcheche.cutprice.list_price_before')}}：</text>
								<text class="t2">
									{{ currency_symbol }}
									<text>{{ item.price }}</text>
								</text>
							</view>
							<view class="price">
								<text class="sales-price">
									{{$t('fengcheche.cutprice.list_price_old_price')}}：
									<text>{{ currency_symbol }}{{ item.min_original_price }}</text>
								</text>
								<view class="gotopin">{{$t('fengcheche.cutprice.list_go_to_order_text')}}</view>
							</view>
						</view>
					</navigator>
				</view>
			</view>
			
			<!-- 分享弹窗 -->
			<component-share-popup :diyTheme="diytheme" ref="share"></component-share-popup>
			
			<!-- 购物车 -->
			<navigator url="/pages/fcc/cutprice/order/order" hover-class="none">
			    <view class="common-quick-nav quick-nav-cart">
			        <image :src="common_static_url+'mini/mycut.png'" class="dis-block"></image>
			    </view>
			</navigator>
			
			<!-- 结尾 -->
			<component-bottom-line :propStatus="data_bottom_line_status" :propMsg="$t('fengcheche.cutprice.data_bottom_line_msg')"></component-bottom-line>
		</block>
		<block v-else>
		    <!-- 提示信息 -->
		    <component-no-data :propStatus="data_list_loding_status" :propMsg="data_list_loding_msg"></component-no-data>
		</block>
    </view>
</template>

<script>
const app = getApp();
import componentBanner from '@/pages/fcc/cutprice/slider/slider';
import componentNoData from "@/components/no-data/no-data";
import componentBottomLine from "@/components/bottom-line/bottom-line";
import { cutprice_base_info } from '@/pages/fcc/cutprice/public/common.js';
import componentSharePopup from "@/components/share-popup/share-popup";

var common_static_url = app.globalData.get_static_url('cutprice', true); 
export default {
    components: {
        componentBanner,
		componentNoData,
		componentBottomLine,
		componentSharePopup
    },
    data() {
        return {
			common_static_url: common_static_url,
            currency_symbol: app.globalData.currency_symbol(),
            load_status: 0,
            data_list_loding_status: 1,
            data_bottom_line_status: false,
            banner_list: [],
            base_info: null,
            data_list: [],
            data_page_total: 0,
            data_page: 1,
            params: null,
            post_data: {},
            common_app_is_online_service: 0,
            data_total: '',
			data_list_loding_msg:'',
			// 自定义分享信息
			share_info: {},
			// 多语言
			lang_info:{},
			diytheme:null
        };
    },
    onLoad(params) {
        this.setData({
            params: params,
            post_data: params
        });
		this.init();
    },
    onReachBottom: function () {
        this.get_data_list(this.data_page);
    },
    // 下拉刷新
    onPullDownRefresh() {
        this.setData({
            data_page: 1
        });
        this.get_data_list(1);
    },
    methods: {
		// 加载之前
		init() {
			// 先获取语言基础信息
			cutprice_base_info()
			.then(data=>{
				this.init_before(data)
				.then(()=>{
					this.init_real();
					this.get_data_list();
				});
			});
		},
		// 设置本页参数
		init_before(data){
			var self = this;
			return new Promise((resolve, reject) => {
				self.setData({
					lang_info: data.cutprice_lang_info,
					currency_symbol: app.globalData.get_config('currency_symbol'),
					common_app_is_online_service: app.globalData.get_config('config.common_app_is_online_service'),
				});
				console.log(data);
				uni.setNavigationBarTitle({
				    title: self.$t('fengcheche.cutprice.app_title')
				});
				resolve();
			});
		},
        // 获取数据列表
        init_real() {
            var self = this; // 加载loding

            this.setData({
                data_list_loding_status: 1
            }); 
			
			// 加载loding
            uni.request({
                url: app.globalData.get_request_url('index', 'index', 'cutprice'),
                method: 'POST',
                data: {},
                dataType: 'json',
                success: (res) => {
                    uni.stopPullDownRefresh();
                    self.setData({
                        load_status: 1
                    });

                    if (res.data.code == 0) {
                        var data = res.data.data;
                        self.setData({
                            data_bottom_line_status: true,
                            banner_list: data.banner_list || [],
                            base_info: data.base || {},
							share_info:{
								'title':data.base.kanjia_name || self.share_info.title,
								'desc':data.base.kanjia_fname || (data.base.kanjia_name || self.share_info.title),
								'img':(data.banner_list.length > 0) ? (data.banner_list[0].images_url || '') : '',
							}
                        });
                    } else {
                        self.setData({
                            data_list_loding_status: 0,
                            data_bottom_line_status: true
                        });
                        app.globalData.showToast(res.data.msg);
                    }
                },
                fail: () => {
                    uni.stopPullDownRefresh();
                    self.setData({
                        data_list_loding_status: 2,
                        data_bottom_line_status: true,
                        load_status: 1
                    });
                    app.globalData.showToast(this.$t('fengcheche.cutprice.request_data_error_tips'));
                }
            });
        },

        // 获取数据列表
        get_data_list(is_mandatory) {
            // 分页是否还有数据
            if ((is_mandatory || 0) == 0) {
                if (this.data_bottom_line_status == true) {
                    return false;
                }
            } // 加载loding

            uni.showLoading({
                title: this.$t('fengcheche.cutprice.loading_tips')
            });
            this.setData({
                data_list_loding_status: 1
            }); // 参数

            var params = this.params;
            var post_data = this.post_data;
            post_data['page'] = this.data_page;
            post_data['status'] = 1; //进行中的
            // 获取数据

            uni.request({
                url: app.globalData.get_request_url('search', 'index', 'cutprice'),
                method: 'POST',
                data: post_data,
                dataType: 'json',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success: (res) => {
                    uni.hideLoading();
                    uni.stopPullDownRefresh();
                    console.log(res);

                    if (res.data.code == 0) {
                        if (res.data.data.data.length > 0) {
                            if (this.data_page <= 1) {
                                var temp_data_list = res.data.data.data;
                            } else {
                                var temp_data_list = this.data_list;
                                var temp_data = res.data.data.data;

                                for (var i in temp_data) {
                                    temp_data_list.push(temp_data[i]);
                                }
                            }

                            this.setData({
                                data_list: temp_data_list,
                                data_total: res.data.data.total,
                                data_page_total: res.data.data.page_total,
                                data_list_loding_status: 3,
                                data_page: this.data_page + 1
                            }); // 是否还有数据

                            if (this.data_page > 1 && this.data_page > this.data_page_total) {
                                this.setData({
                                    data_bottom_line_status: true
                                });
                            } else {
                                this.setData({
                                    data_bottom_line_status: false
                                });
                            }
                        } else {
                            this.setData({
                                data_list_loding_status: 0
                            });

                            if (this.data_page <= 1) {
                                this.setData({
                                    data_list: [],
                                    data_bottom_line_status: false
                                });
                            }
                        }
                    } else {
                        this.setData({
                            data_list_loding_status: 0
                        });
                        app.globalData.showToast(res.data.msg);
                    }
                },
                fail: () => {
                    uni.hideLoading();
                    uni.stopPullDownRefresh();
                    this.setData({
                        data_list_loding_status: 2
                    });
                    app.globalData.showToast(this.$t('fengcheche.cutprice.request_data_error_tips'));
                }
            });
        },
		
		// 分享开启弹层
		popup_share_event(e) {
		    if ((this.$refs.share || null) != null) {
		        this.$refs.share.init({
		            status: true,
		            share_info: this.share_info
		        });
		    }
		},

        // 滚动加载
        scroll_lower(e) {
            this.get_data_list();
        },

        // 导航事件
        navigation_event(e) {
            app.globalData.operation_event(e);
        }
    }
};
</script>
<style>
@import './index.css';
</style>
